package com.yeyuanqi.hottop.untils;


import com.yeyuanqi.hottop.vo.HotListVO;
import com.yeyuanqi.hottop.vo.NameAndValueVO;
import com.yeyuanqi.hottop.vo.PieChartVO;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;


/**
 * 封装了一个用于生成饼图所需数据格式的方法
 * @author yeyuanqi
 * @date 2022/01/22
 **/
@Component
public class PieChartDataUtil {

    /**
     * 从redis获取的每日热搜top1数据转换成list<NameAndValueVO>类型
     * @return 返回一个List<NameAndValueVO>列表
     */
    public PieChartVO getPieHotData(List<HotListVO> hotLists){
        PieChartVO pieChartVO = new PieChartVO();
        List<String> urlList = new ArrayList<>();
        List<String> topicList = new ArrayList<>();
        List<NameAndValueVO>  dataList= new ArrayList<>();
        for (HotListVO hotlist : hotLists){
            NameAndValueVO data =new NameAndValueVO();
            data.setName(hotlist.getTopic());
            data.setValue(hotlist.getHot());
            dataList.add(data);
            topicList.add(hotlist.getTopic());
            urlList.add(hotlist.getUrl());
        }
        pieChartVO.setPiedata(dataList);
        pieChartVO.setUrl(urlList);
        pieChartVO.setTopic(topicList);
        return pieChartVO;
    }
}
